package com.ml.shs.controller;

import com.ml.shs.annotation.LoginAnnotation;
import com.ml.shs.entity.Goods;
import com.ml.shs.repository.GoodsRepository;
import com.ml.shs.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shs/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @PostMapping("/getList")
    public List<Goods> getGoodsList(@RequestBody Map params) {
        return goodsService.getList(params.get("classifyId").toString());
    }

    @GetMapping("getListByUser")
    public List<Goods> getListByUser(HttpSession session) {
        String userId = (String)session.getAttribute("userId");
        return goodsService.getListByUser(userId);
    }

    @PostMapping("/save")
    @LoginAnnotation(name="没有登陆")
    public String add(@RequestBody Goods goods) {
        goods.setCreateTime(new Date());
        goodsService.save(goods);
        return "success";
    }

    @PostMapping("/completion")
    @LoginAnnotation(name="没有登陆")
    public String completion(@RequestBody Goods goods) throws Exception {
        goodsService.completion(goods.getId());
        return "success";
    }

    @PostMapping("/delete")
    @LoginAnnotation(name="没有登陆")
    public String delete(@RequestBody Goods goods) throws Exception{
        goodsService.delete(goods.getId());
        return "success";
    }

}
